package com.fessc.develop.handler.processor.callback;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fessc.develop.base.CCBResult;
import com.fessc.develop.entity.ccbvo.CCBBankCallBackVo;
import com.fessc.develop.entity.ccbvo.CCBResponseBodyVo;
import com.fessc.develop.entity.ccbvo.CCBSettleTradeCallBackVo;
import com.fessc.develop.entity.ccbvo.CCBSettleTradeOrderVo;
import com.fessc.develop.model.enums.EnumServiceCode;
import com.fessc.develop.handler.processor.request.CCBIBaseService;
import com.fessc.develop.utils.BusiUtil;
import com.fessc.develop.utils.ExceptionUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
public class CCBSettleTradeCallBackService implements CCBIBaseService {

    @Resource
    private List<CCBICallBackProcessor> callBackProcessorList;

    private Map<String, CCBICallBackProcessor> callBackProcessorMap;

    @PostConstruct
    void init() {
        if (BusiUtil.isNotNull(callBackProcessorList)) {
            callBackProcessorMap = callBackProcessorList.stream().collect(Collectors.toMap(CCBICallBackProcessor::getServiceCode, p -> p, (v1, v2) -> v1));
        } else {
            callBackProcessorMap = new HashMap<>();
        }
    }

    @Override
    public String getServiceCode() {
        return EnumServiceCode.CCB_HMT006.toString();
    }

    @Override
    public CCBResult<CCBResponseBodyVo> process(String requestBody) {
        log.info("建行跨境付汇回调通知请求报文：" + requestBody);
        CCBResult<CCBResponseBodyVo> result;
        CCBBankCallBackVo ccbBankCallBackVo = JSON.parseObject(requestBody, CCBBankCallBackVo.class);
        JSONObject jsonObject = JSONObject.parseObject(requestBody);
        String sumOrderNo = jsonObject.getJSONObject("body").getString("SumOrdrNo");  //汇总订单编号
        JSONArray bodyOrdrNos = jsonObject.getJSONObject("body").getJSONArray("OrdrNos");  //明细订单编号数组
        if (BusiUtil.isNull(bodyOrdrNos) || bodyOrdrNos.isEmpty()) {
            throw ExceptionUtil.businessException("EE1009");
        }
        List<CCBSettleTradeOrderVo> ordrNos = JSONObject.parseArray(bodyOrdrNos.toJSONString(), CCBSettleTradeOrderVo.class);
        CCBICallBackProcessor callBackProcessor = callBackProcessorMap.get(ccbBankCallBackVo.getCcbCallBackHeadVo().getMsgBsnTp());
        if (BusiUtil.isNull(callBackProcessor)) {
            throw ExceptionUtil.businessException("EE4012");
        }
        CCBSettleTradeCallBackVo ccbSettleTradeCallBackVo = JSON.parseObject(ccbBankCallBackVo.getCcbCallBackBodyVo().toString(), CCBSettleTradeCallBackVo.class);
        ccbSettleTradeCallBackVo.setOrdrNos(ordrNos);
        ccbSettleTradeCallBackVo.setOrdrNo(sumOrderNo);
        result = callBackProcessor.settleTradeStatusProcess(ccbSettleTradeCallBackVo);
        return result;

    }
}
